package com.fofia.iotweb.framework.core;

import cn.hutool.log.StaticLog;
import com.mybatisflex.core.audit.AuditManager;
import com.zaxxer.hikari.HikariDataSource;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import javax.sql.DataSource;

@Configuration
public class MybatisFlexConfig{

    //@Bean
    public MybatisFlexConfig(){
//        AuditManager.setAuditEnable(true);
//        //设置 SQL 审计收集器
//        AuditManager.setMessageCollector(auditMessage ->
//                StaticLog.info("{},{}ms", auditMessage.getFullSql()
//                        , auditMessage.getElapsedTime())
//        );
    }

    //此下的 db1 与 mybatis.db1 将对应在起来
    // 可以用 @Db("db1") 注入mapper
    //typed=true，表示默认数据源。@Db 可不带名字注入
    @Bean(value = "db", typed = true)
    public DataSource db(@Inject("${iot.db}") HikariDataSource ds) {
        ds.setMaximumPoolSize(10);
        return ds;
    }

    /**
     * 反刍标签数据库连接
     * @param ruminateDs
     * @return
     */
    @Bean(value = "fctag")
    public DataSource ruminateTag(@Inject("${iot.fctag}") HikariDataSource ruminateDs) {
        ruminateDs.setMaximumPoolSize(10);
        return ruminateDs;
    }
    /**
     * 智能标签数据库连接
     * @param tagDs
     * @return
     */
    @Bean(value = "tag")
    public DataSource smartTag(@Inject("${iot.tag}") HikariDataSource tagDs) {
        tagDs.setMaximumPoolSize(10);
        return tagDs;
    }

    /**
     * 智能标签数据库连接
     * @param gatewayDs
     * @return
     */
    @Bean(value = "gateway")
    public DataSource gatewayDb(@Inject("${iot.gateway}") HikariDataSource gatewayDs) {
        gatewayDs.setMaximumPoolSize(10);
        return gatewayDs;
    }

}